#include "my_basic_timer.h"


static TIM_TimeBaseInitType TIM_TimeBaseStructure;
static TIM_TimeBaseInitType TIM_Time6_base_str;

static OCInitType TIM_OCInitStructure;
static uint16_t PrescalerValue = 0;

/**
 * @brief  Configures tim3 clocks.
 */
static void basic_timer_cfg(void)
{
    /* Compute the prescaler value */
    PrescalerValue = 0;//(SystemCoreClock / 640000) - 1; //(uint16_t) (SystemCoreClock / 12000000) - 1;

    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);    
	TIM_TimeBaseStructure.Period    = 65535;//1000/2 -1;//100;    //    1ms
   // TIM_TimeBaseStructure.Period    = 100 ; 
	
    TIM_TimeBaseStructure.Prescaler = 0;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM3, &TIM_TimeBaseStructure);

    /* Prescaler configuration */
    TIM_ConfigPrescaler(TIM3, PrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    /* TIM3 enable update irq */
    TIM_ConfigInt(TIM3, TIM_INT_UPDATE, ENABLE);

    /* TIM3 enable counter */
    TIM_Enable(TIM3, ENABLE);

    //---------timer 6 定时 1秒，做系统计时使用。
    PrescalerValue = (SystemCoreClock / (1000 - 1));
    TIM_InitTimBaseStruct(&TIM_Time6_base_str);    
	TIM_Time6_base_str.Period    = 1010;//1000/2 -1;//100;    //  1s
  
	
    TIM_Time6_base_str.Prescaler = 0;
    TIM_Time6_base_str.ClkDiv    = 0;
    TIM_Time6_base_str.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM6, &TIM_Time6_base_str);

    /* Prescaler configuration */
    TIM_ConfigPrescaler(TIM6, PrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    /* TIM3 enable update irq */
    TIM_ConfigInt(TIM6, TIM_INT_UPDATE, ENABLE);

    /* TIM3 enable counter */
    TIM_Enable(TIM6, ENABLE);


}
/**
 * @brief  Configures the different system clocks.
 */
static void basic_timer_rcc_cfg(void)
{
    /* PCLK1 = HCLK/4 */
    RCC_ConfigPclk1(RCC_HCLK_DIV1);	//RCC_HCLK_DIV1

    /* TIM3 clock enable */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);

    /* TIM6 clock enable */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM6, ENABLE);
}


/**
 * @brief  Configure the nested vectored interrupt controller.
 */
static void basic_timer_nvic_cfg(void)
{
    NVIC_InitType NVIC_InitStructure;

    /* Enable the TIM1 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;//TIM8_UP_IRQn  TIM1_UP_IRQn
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 2;	//1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    /* Enable the TIM6 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM6_IRQn;//TIM8_UP_IRQn  TIM1_UP_IRQn
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 2;	//1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

}

void my_basic_timer_init(void)
{
    basic_timer_rcc_cfg();
    basic_timer_nvic_cfg();
    basic_timer_cfg();
}

